import router from '../router'

import axios from 'axios'

// axios.defaults.withCredentials = true;

// Vue.prototype.$message = Message
const request = axios.create({
    baseURL: window._apiUrl.webUrl,
    timeout: 300000,
    headers: {
        'Content-type': 'application/json',
    }
})

//form request
const requestByForm = axios.create({
    baseURL: window._apiUrl.webUrl,
    timeout: 20000,
    headers: {
        'Content-Type': 'multipart/form-data'
    }
})

var requestByImage = axios.create({
    baseURL: window._apiUrl.webUrl,
    timeout: 10000,
    responseType: 'blob',
    headers: {
       
    }
})

export function getImage(url,params) {
    requestByImage.defaults.headers.Authorization = localStorage.getItem('token');
    return new Promise((resolve, reject) => {
        let reqUrl = url;
        if(params!=undefined&&params!=null){
            reqUrl = url+params;
        }
        requestByImage.get(reqUrl).then(res=>{
            resolve(res.data)
        }).catch(err=>{
            ElMessage({message:'服务器异常，请稍后重试', type: 'error'});
        })
    })
}

export function get(url, params) {
    request.defaults.headers.Authorization = localStorage.getItem('token');
    return new Promise((resolve, reject) => {
        request.get(url, {
            params: params
        }).then(res => {
            if(res!=undefined&&res.headers['devcat-token']){
                localStorage.setItem('token',res.headers['devcat-token'])
            }
            resolve(res.data);
        }).catch(err => {
            if(err.response != undefined && err.response.status==401){
                loginError(err.response.data.data)
                router.push('/login')
            }   
            if(err.response != undefined &&err.response.status==503){
                loginError(err.response.data.path+"服务正在维护，请稍后重试")
            }
            reject(err.data)
        })
    });
}

export function rest(url, params) {
    request.defaults.headers.Authorization = localStorage.getItem('token');
    return new Promise((resolve, reject) => {
        request.get(url + '/' + params).then(res => {
            resolve(res.data);
        }).catch(err => {
            if(err.response.status==401){
                loginError(err.response.data.data)
                router.push('/login')
            }
            if(err.response.status==503){
                loginError(err.response.data.path+"服务正在维护，请稍后重试")
            }
            reject(err.data)
        })
    });
}

export function post(url, params) {
    request.defaults.headers.Authorization = localStorage.getItem('token');
    return new Promise((resolve, reject) => {
        request.post(url, JSON.stringify(params))
            .then(res => {
                if(res && res.headers['devcat-token']){
                    localStorage.setItem('token',res.headers['devcat-token'])
                }
                resolve(res.data);
            })
            .catch(err => {
                if(err.response.status==401){
                    loginError(err.response.data.data)
                    router.push('/login')
                }
                if(err.response.status==503){
                    loginError(err.response.data.path+" 服务正在维护或重启，请稍后重试")
                }
                reject(err)
            })
    });
}
export function del(url, params) {
  request.defaults.headers.Authorization = localStorage.getItem('token');
  return new Promise((resolve, reject) => {
      request.delete(url, { data: params }).then(res => {
          if(res.headers['devcat-token']){
          localStorage.setItem('token',res.headers['devcat-token'])
          }
          resolve(res.data);
      }).catch(err => {
          reject(err);
      })
  })
}

export function put(url, params) {
  request.defaults.headers.Authorization = localStorage.getItem('token');
  return new Promise((resolve, reject) => {
      request.put(url, JSON.stringify(params)).then(res => {
         if(res.headers['devcat-token']){
          localStorage.setItem('token',res.headers['devcat-token'])
          }
          resolve(res.data);
      }).catch(err => {
          reject(err);
      })
  })
}

export function formPost(url, formData) {
    requestByForm.defaults.headers.Authorization = localStorage.getItem('token');
    return new Promise((resolve, reject) => {
        requestByForm.post(url, formData).then(res => {
            if(res.headers['devcat-token']){
                localStorage.setItem('token',res.headers['devcat-token'])
            }
            resolve(res)
        }).catch(res => {
            reject(res.data)
        })
    });
}


request.interceptors.response.use((res)=>{
    if(!res.data.status && res.data.statusCode == 10003){
        localStorage.setItem("loginStatus","no")
    }else{
        return res
    }
})
